package com.example.myapplication;

import com.example.myapplication.shortvideo.utils.GsonUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Xinwen {
    int index;

    public static void main(String[] args) {
        Xinwen jinPai = new Xinwen();
        jinPai.query();
//        new Timer().schedule(new TimerTask() {
//            @Override
//            public void run() {
//                try {
//                    Xinwen jinPai = new Xinwen();
//                    jinPai.query();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }, 0, 60 * 1000);
    }

    public static class Cos {
    }

    public void query() {
        String url = "https://www.163.com/dy/media/T1603594732083.html";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
//                System.out.println(str);
                fenxi(str);
//                save();
//                upLoad();
            }
        });
    }

    public void query2(String url) {
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                fenxi2(str);
//                save();
//                upLoad();
            }
        });
    }


    public void fenxi(String html) {
        int idex1 = html.indexOf("<li class=\"media_article\">");
        if (idex1 <= 0) {
            return;
        }
        String htm11 = html.substring(idex1);
        String tag2 = "  <a href=\"";
        int index2 = htm11.indexOf(tag2);
        String html2 = htm11.substring(index2 + tag2.length());
        String tag3 = "\" class=\"media_article_img\">";
        int index3 = html2.indexOf(tag3);
        String html3 = html2.substring(0, index3);
        query2(html3);
    }

    public void fenxi2(String html) {
        String tag1 = "<br/>1、";
        int idex1 = html.indexOf(tag1);
        String htm11 = html.substring(idex1 + "<br/>".length());
        String tag2 = "</p>";
        int index2 = htm11.indexOf(tag2);
        String html2 = htm11.substring(0, index2);
        html2 = html2.replace("<br/>", "\n");
        String[] array = html2.split("\n");
        List<String> listA = Arrays.asList(array);
        ArrayList arrList = new ArrayList(listA);
        arrList.remove(arrList.size() - 1);
        save(arrList);
    }


    public void save(List<String> listA) {
        String json = GsonUtil.toJson(listA);
        try {

            File file = new File("D:\\downloads\\putjson\\news.json");

            //if file doesnt exists, then create it
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(json);
            bw.close();

            System.out.println("保存成功");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
